﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;
using System.Data;


namespace HRM
{
    public partial class frmDepartmentList : FormBase
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _DepID = 0;
        int _MasterID = 0;
        int _BranchID = 0;
        BranchClient _wcfBranch;
        DepartmentClient wcfDepartment;
        RoleRightInfo listRole;
        MainServiceClient _service;
        PositionDetailClient _wcfPD;

        public frmDepartmentList()
        {
            InitializeComponent();
            LoadLanguage();
            _service = Service.MainService();
            _wcfPD = Service.PositionDetail();
            _wcfBranch = Service.Branch();
            wcfDepartment = Service.Department();
            listRole = UIParamater.Quyen_PhongBan;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole._delete)
                btnDel.Enabled = false;
            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                btnEdit.Text = "xem";
            }
            UISetting.LoadTreeViewHRMProfile(ref tvDepartment);
            this.KeyPreview = true;
        }

        public override void Show_Form()
        {
            this.Show();
        }

        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_ContextMenuStrip(ref mnuPopup);
        }



        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (tvDepartment.SelectedNode.Level == 1)
            {
                frmDepartmentInfo f = new frmDepartmentInfo(_BranchID, 0, 0, listRole);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    tvDepartment.SelectedNode.Nodes.Add(f.DeptID.ToString(), f.DeptName, 2, 3).Tag = f.DeptID;
                }
            }
            else 
            {
                if (tvDepartment.SelectedNode.Level >= 2)
                {
                    frmDepartmentInfo f = new frmDepartmentInfo(_BranchID, 0, _MasterID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        tvDepartment.SelectedNode.Nodes.Add(f.DeptID.ToString(), f.DeptName, 2, 3).Tag = f.DeptID;
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (listRole.Update || listRole.View)
            {
                if (tvDepartment.SelectedNode.Level == 2)
                {
                    frmDepartmentInfo f = new frmDepartmentInfo(_BranchID, _DepID, 0, listRole);
                    f.ShowDialog(this);
                    //if (f.ShowDialog(this) == DialogResult.OK)
                    //{
                        tvDepartment.SelectedNode.Text = f.DeptName;
                    //}
                }
                else
                {
                    if (tvDepartment.SelectedNode.Level >= 3)
                    {
                        frmDepartmentInfo f = new frmDepartmentInfo(_BranchID, _DepID, _MasterID, listRole);
                        f.ShowDialog(this);
                        //if (f.ShowDialog(this) == DialogResult.OK)
                        //{
                            tvDepartment.SelectedNode.Text = f.DeptName;
                        //}
                    }
                }
            }
            else
                MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (!listRole.Delete)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_DepID == 0)
            {
                MessageBox.Show("Đây là chi nhánh. Bạn không xóa được tại chức năng này.", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (wcfDepartment.Department_Using(_DepID) != 0)
            {
                MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (_wcfPD.PositionDetail_CountEmployeeWithDept(_DepID))
                    Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoanhanvientruoc"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                else
                {
                    if (tvDepartment.SelectedNode.Nodes.Count > 0)
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoabophanthanhvientruoc"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    else
                        if (wcfDepartment.Department_Delete(_DepID))
                        {
                            Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                            tvDepartment.SelectedNode.Remove();
                        }
                        else
                        {
                            MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                }
            }
        }



        private void frmDepartmentList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void btnNewPopUp_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditPopUp_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void btnDelPopUp_Click(object sender, EventArgs e)
        {
            btnDel_Click(null, null);
        }
        private void frmDepartmentList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
        }

        private void tvDepartment_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                _BranchID = Convert.ToInt32(e.Node.Tag);
                _MasterID = 0;
                _DepID = 0;
            }
            else if (e.Node.Level == 2)
            {
                _DepID = Convert.ToInt32(e.Node.Tag);
                _BranchID = Convert.ToInt32(e.Node.Parent.Tag);
                _MasterID = Convert.ToInt32(e.Node.Tag);
            }
            else if (e.Node.Level > 2)
            {
                _DepID = Convert.ToInt32(e.Node.Tag);
                _MasterID = Convert.ToInt32(e.Node.Parent.Tag);
                _BranchID = Convert.ToInt32(GetBranchNode(e.Node).Tag);
            }

        }
        TreeNode GetBranchNode(TreeNode node)
        {
            TreeNode retVal = node;
            if (node.Level >= 2)
                retVal = node.Parent;
            return retVal;
        }

        private void tvDepartment_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                tvDepartment.SelectedNode = tvDepartment.HitTest(e.X, e.Y).Node;
        }

        private void frmDepartmentList_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfBranch.Close();
            _wcfPD.Close();
        }

        public  void ReloadDepartmentList()
        {
            UISetting.LoadTreeViewHRMProfile(ref tvDepartment);
        }
    }
}
